package cn.wolfcode.luowowo.member.service.impl;


import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.exception.LogicException;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.member.util.AssertUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

import static cn.wolfcode.luowowo.member.util.Consts.VERIFY_CODE_VAI_TIME;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;//这里使用Autowired,因为这里是同一个项目,不是调用别的项目的mapper文件

    @Reference
    private IUserInfoRedisService userInfoRedisService;//这里是别的项目的,所以我们需要贴@Reference注解

    @Override
    public UserInfo getUserInfo(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean getPhone(String phone) {
        return userInfoMapper.getPhone(phone) > 0;
    }

    @Override
    public String sendVerifyCode(String phone) {
        //通过UUID得到验证码,也就是创建验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        //将验证码缓存在redis上
        String key = RedisKeys.VERIFY_CODE.join(phone);//使用一个枚举工具类拼接key和保存时间
//        String key = "verify_code" + phone;
        //将数据添加到redis库中
        userInfoRedisService.setVerifyCode(key, code, RedisKeys.VERIFY_CODE.getTime());
        System.out.println("您的验证码是:" + code + "请再" + VERIFY_CODE_VAI_TIME + "分钟之内使用,否则失效");
        return null;
    }

    @Override
    public void userRedis(String phone, String nickname, String password, String rpassword, String verifyCode) throws LogicException {
        //判断所有参数是否为空
        AssertUtil.hasLength(phone, "手机号码不能为空");
        AssertUtil.hasLength(nickname, "昵称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");
        //判断2次密码输入是否相同
        AssertUtil.isEquals(password, rpassword, "密码不一致");
        //判断手机号码是否唯一
        if (this.getPhone(phone)) {
            throw new LogicException("手机号已经被注册");
        }
        //判断验证码是否正确
        String key = RedisKeys.VERIFY_CODE.join(phone);
//        String key = "verify_code" + phone;
        String code = userInfoRedisService.getVerifyCode(key);
        //判断验证码是否为空,为空的话验证码失效,里面的方法equalsIgnoreCase表示忽略大小写
        System.out.println(code.toString());
        if (code == null || !code.equalsIgnoreCase(verifyCode)) {
            throw new LogicException("验证码已经失效或者验证码错误");

        }
        //注册成功
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setPassword(password);//我们要记得加密
        userInfo.setNickname(nickname);
        userInfo.setState(UserInfo.STATE_NORMAL);//设置状态
        userInfo.setLevel(1);
        userInfo.setHeadImgUrl("/images/default.jpg");
        //记得往数据库里面添加
        userInfoMapper.insert(userInfo);
    }

    @Override
    public String userLogin(String username, String password) throws LogicException {
        //记得检查

        //检查用户是否存在
        UserInfo userInfo = userInfoMapper.selectByUsernameAndPassword(username, password);
        if (userInfo == null) {
            throw new LogicException("账号或密码错误");
        }
        //创建token,存在redis中
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        //记得加前缀
        String key = RedisKeys.USER_LOGIN.join(token);
        //然后缓存到redis中
        userInfoRedisService.addToken(key, userInfo);
        return key;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }


}
